package com.fjxd.ticketStore.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fjxd.ticketStore.pojo.TicketStore;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author csw
 */
@Mapper
public interface TicketStoreMapper extends BaseMapper<TicketStore> {
    /**
     * 基于机票id与机票类型扣减机票库存
     *
     * @param ticketId    机票id
     * @param ticketType  机票类型
     * @param ticketCount 购买机票的数量
     */
    @Update("update tb_ticket_store set ticket_count = case when ticket_count>=#{ticketCount} " +
            "then ticket_count-#{ticketCount} end " +
            "where ticket_id = #{ticketId} and ticket_type=#{ticketType}")
    void deduct(String ticketId, Integer ticketType, Integer ticketCount);

    /**
     * 通过机票类型表总id回增机票库存
     *
     * @param aircraftTicketId order订单中的机票库存id
     * @param ticketCount      机票库存回增的数量
     */
    @Update("update tb_ticket_store set ticket_count = ticket_count + #{ticketCount} " +
            "where id = #{aircraftTicketId}")
    void goBack(Integer aircraftTicketId, Integer ticketCount);

    /**
     * 通过机票id对应的机票座位余数
     *
     * @param ticketId 机票id
     * @return 返回机票id对应的机票座位余数
     */
    @Select("select * from tb_ticket_store where ticket_id = #{ticketId}")
    List<TicketStore> getTicketCount(Integer ticketId);

}
